# 内置函数 🏀 abs, max, min
# 查看所有的内置函数
print(dir(__builtins__))

abs(-1.1)  # 1.1 取绝对值
print(sum([1, 2, 3]))  # 6
print(min(1, 2, 3))  # 1
print(max(1, 2, 3))  # 3

print(max(-8, 5, key=abs))  # -8

# 内置函数 🏀 zip: 将可迭代对象作为参数, 将对象中对应的元素打包成一个元祖
# 按照长度最短的列表组合
li1 = [1, 2, 3, 9]
li2 = ["a", "b", "c", "d"]
print(zip(li1, li2))  # <zip object at 0x000002C9DA595EC0>
for item in zip(li1, li2):
    print(item)  # (1, a) (2, b) (3, c)
    print(item[0])
    print(item[1])

print(list(zip(li1, li2)))  # [(1, 'a'), (2, 'b'), (3, 'c'), (9, 'd')]


# 内置函数 🏀 map: 可以对可迭代对象中的每一个元素进行映射，分别去执行
# 列表中的每一个元素都会去执行 fun
# def printEle(x):
#     return x * 5

multiple5Time = lambda x: x * 5


li4 = [1, 2, 3, 4]
mp4 = map(multiple5Time, li4)
mp5 = map(multiple5Time, li4)
for item in mp4:
    print(item)
print(list(mp5))  # [5, 10, 15, 20]


# 内置函数 🏀 reduce: 先把对象中的两个元素取出，计算出一个值然后保存, 接下来把这个计算值和第三个元素进行计算
from functools import reduce

li6 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

add = lambda x, y: x + y

res = reduce(add, li6)
print(res)  # 55

#####
#####
#####
print("-------------拆包----------------------")
# 拆包: 函数中的多个返回数据，去掉元组，列表，字典直接获取数据的过程
tuple_a = (1, 2, 3, 4)
print(tuple_a)
a, b, c, d = tuple_a  # 元组内的个数与接收的变量数相同
print(a)
print(b)
print(c)
print(d)

a, *b = tuple_a
print(a)  # 1
print(b)  # [2,3,4]
print(type(b))  # <class 'list'>


### demo 2: 可变参数
def foo(a, b, *args):
    print(a, b)
    print(args, type(args))


foo(1, 2, 3, 4, 5, 6, 7)  # (3, 4, 5, 6, 7) <class 'tuple'>

arg = (1, 2, 3, 4, 5, 6, 7)
foo(*arg)  # (3, 4, 5, 6, 7) <class 'tuple'>


### Demo
tuple_b = (1, 2, 3, 4, 5, 6, 7, 8, 9)

a1, *b1 = tuple_b
print(a1)  # 1
print(b1)  # [2, 3, 4, 5, 6, 7, 8, 9]

a2, *b2 = b1
print(a2, b2)  # 2 [3, 4, 5, 6, 7, 8, 9]

a3, *b3 = b2
print(a3, b3)  # 3 [4, 5, 6, 7, 8, 9]

a4, a5, a6, a7, a8, *b9 = b3
print(a4, a5, a6, a7, a8, b9)  # 4 5 6 7 8 [9]
a9, *b10 = b9
print(a9)  # 9
print(b10)  # []
